/**
 * 文件名：SmsMessageApiImpl.java
 *
 * 创建人：虞云波 - .com
 *
 * 创建时间：2017年11月1日 下午3:09:49
 *
 * 版权所有：江苏晟邦网络科技有限公司
 */
package com.cbwl.eoas.api.service.impl.sysconfig;

import java.util.Date;
import java.util.List;

import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import com.cbwl.eoas.api.client.service.sysconfig.ISmsMessageApi;
import com.cbwl.eoas.cms.api.eoas.IAppSmsLogService;
import com.cbwl.eoas.cms.api.sysconfig.ISysUserService;
import com.cbwl.eoas.cms.model.eoas.AppSmsLogDTO;
import com.cbwl.eoas.cms.model.sysconfig.SysUserDTO;
import com.cbwl.eoas.common.file.SendMessagePropertiesUtil;
import com.cbwl.eoas.common.util.constain.DicDetailConstant;
import com.cbwl.eoas.common.util.jdkcustom.RandomCreateString;
import com.cbwl.eoas.common.util.sms.SmsMessageUtil;
import com.cbwl.eoas.common.web.Result;


/**
 * <p>[描述信息：说明类的基本功能]</p>
 *
 * @author 虞云波 - yuyunbo@3856.cc
 * @version 1.0 Created on 2017年11月1日 下午3:09:49
 */
@Service
public class SmsMessageApiImpl implements ISmsMessageApi {
	
	
	private static Logger _log = LoggerFactory.getLogger(SmsMessageApiImpl.class);
	
	@Autowired
	private IAppSmsLogService appSmsLogService;
	
	@Autowired
	private SmsMessageUtil smsMessageUtil;
	
	@Autowired
	private ISysUserService sysUserService;
	

	/**
	 * <p>功能实现描述</p>
	 * 
	 * @see com.cbwl.eoas.api.client.service.sysconfig.ISmsMessageApi#saveAndSendMessage(com.cbwl.eoas.cms.model.eoas.AppSmsLogDTO)
	 * @author: 虞云波 - yuyunbo@3856.cc 
	 * @throws Exception 
	 * @date: Created on 2017年11月1日 下午3:09:49
	 */
	@Override
	public Result saveAndSendMessage(AppSmsLogDTO bean,SysUserDTO userBean) throws Exception {
		Result result = new Result(Result.Status.OK, "请求成功", "200");
		
		if(!StringUtils.isEmpty(userBean.getUserJobno())){
			//校验工号和手机号码是否匹配
			userBean.setUserPhone(bean.getSmsTelephone());
			List<SysUserDTO> userlist =  sysUserService.queryObjectList(userBean);
			if(userlist == null ||  userlist.size()==0){
				result.setCode("CBWL-000016");
				return result;
			}
		}
		
		//校验短信发送是否超过次数
		List<AppSmsLogDTO> list = appSmsLogService.queryObjectList(bean);
		if(list !=null && list.size()>5){
			result.setCode("CBWL-000005");
			return result;
		}
		
		//发送短信信息
		String content = SendMessagePropertiesUtil.getValue("sm_code");
		String code = RandomCreateString.getRandomNum(6);
		content =content.replaceAll("@_@",code);
		
		_log.info("短信内容==="+content);
		String returnstr = smsMessageUtil.sendMessageSingle(bean.getSmsTelephone(),content);
		bean.setSmsReturncode(returnstr);
		bean.setSmsCode(code);
		bean.setSmsSrc(DicDetailConstant.sms_src_1);
		bean.setSmsContent(content);
		bean.setSmsTime(new Date());
		
		appSmsLogService.saveObject(bean);
		
		return result;
	}

	
	/**
	 * 
	 * <p>短信验证码校验</p>
	 * 
	 * @see com.cbwl.eoas.api.client.service.sysconfig.ISmsMessageApi#checkCode(com.cbwl.eoas.cms.model.sysconfig.SysResourceDTO)
	 * @author: 虞云波 - yuyunbo@3856.cc 
	 * @date: Created on 2017年11月1日 下午3:49:57
	 */
	@Override
	public Result checkCode(AppSmsLogDTO bean) {
		
		Result result = new Result(Result.Status.OK, "请求成功", "200");
		//获取发送的验证码
		List<AppSmsLogDTO> list = appSmsLogService.queryObjectList(bean);
		if(list !=null && list.size()>0){
			
		}else{
			result.setCode("CBWL-000013");
		}
		return result;
	}
	
	
	public static void main(String[] args) {
		String content = SendMessagePropertiesUtil.getValue("sm_code");
//		String code = RandomCreateString.getRandomNum(6);
//		content =content.replaceAll("@_@", content);
		System.out.println(content);
	}
	
	

}
